* Figure A10:  Top and Bottom Quartile Age-Log Income Profiles in NLS66 and NLSY79

clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if miss0 | miss1
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 

keep if women==1

* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=weight]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=weight]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=weight]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=weight]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(weight)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1 



forv a=21/50 { 
	gen inc`a' = .
	gen inc_b`a' = .
	forv by = 1948/1953 {
		local y = `by'+`a'
		cap replace inc`a' = faminc_daughter_`y' if byear==`by' & survey==1966 & women==1
	}
	
	
	forv by = 1961/1964 {
		local y = `by'+`a'
		cap replace inc`a' = faminc`y' if byear==`by' & survey==1979
	}
}



keep survey surv79 weight faminc1 inc21-inc50 rank1 id_son id_daughter CASEID
gen row=_n
reshape long inc , i(row) j(age)


replace inc = . if inc<1500
replace inc = . if inc>500000
gen linc = log(inc)


gen quartile = 1 if rank<=25
 replace quartile = 2 if rank>25 & rank<=50
 replace quartile = 3 if rank>50 & rank<=75
 replace quartile = 4 if rank>75 & rank<=100



bys row: egen seq=seq()
gen has_inc = linc<.
bys row: egen obs = sum(has_inc)


graph twoway (qfit linc age if surv79==0 & quartile==1 & obs>=10 [w=weight], lpattern(dash)) (qfit linc age if surv79==1 & quartile==1 & obs>=10 [w=weight]) ///
			 (qfit linc age if surv79==0 & quartile==4 & obs>=10 [w=weight], lpattern(dash)) (qfit linc age if surv79==1 & quartile==4 & obs>=10 [w=weight]) ///
			 , legend(label(1 "1966, Q1") label(2 "1979, Q1")  ///
			 label(3 "1966, Q4") label(4 "1979, Q4")) ///
	graphregion(color(white)) xtitle("Age") ytitle("Log Income") 
graph export ${results_dir}figureA10.png, replace








exit
